<?php

namespace App\Http\Controllers\Admin\Cashier;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cashier\Store;
use App\Models\Cashier\Purchase;

class StoreController extends Controller
{
    public function __construct()
    {
        view()->share([
            '_cashier' => 'active open',
            '_store' => 'active',
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = function ($query) use ($request) {
            if ($request->has('keyword') and $request->keyword != '') {
                $search = "%" . $request->keyword . "%";
                $query->where('name', 'like', $search);
            }

            if ($request->has('key') and $request->key != '') {
                $search = "%" . $request->key . "%";
                $query->where('phone', 'like', $search);
            }
        };
        $stores = Store::with('purchase')->where($where)->orderBy('id', 'desc')->paginate(env('pageSize'));
//        return $stores;
        return view('admin.cashier.store.index', compact('stores'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.cashier.store.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        Store::create($request->all());
        return redirect(route('cashier.store.index'))->with('success', '新增门店成功~');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //按月份分组查询每个门店每个月的进货记录
        $store = Store::with('purchase')->find($id);
//        $store = Store::with('product')->groupBy(date('created_at'))->find($id);
//        return $store;

        return view('admin.cashier.store.show', compact('store'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $store = Store::find($id);

        return view('admin.cashier.store.edit', compact('store'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $store = Store::find($id);
        $store->update($request->all());
        return redirect(route('cashier.store.index'))->with('success', '编辑门店成功~');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Store::destroy($id);
        return back()->with('success', '删除供货商成功~');
    }

    /** 删除多条
     * @param Request $request
     */
    public function delete_more(Request $request)
    {
        $checked_id = $request->checked_id;
//        return $checked_id;

//        foreach ($checked_id as $id) {
        Store::destroy($checked_id);
//        }

    }
}
